package com.ydzt.fanka.server.utils;

import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Convert {
	private static Format f;
	
	/* *
	 * 计算今天是今年的第几周
	 * */
	public static int getWeekWithDayInYear(){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date;
        Calendar calendar = Calendar.getInstance();
		try {
			date = format.parse(format.format(System.currentTimeMillis()));
	        calendar.setFirstDayOfWeek(Calendar.MONDAY);
	        calendar.setTime(date);
		} catch (ParseException e) {
		}
        return calendar.get(Calendar.WEEK_OF_YEAR);
	}
	
	public static String getDrTmpPath(String path,String dr_node){
		String path_tmp = "";
		String arr[] = path.split("/");
		for(int i = 0; i < arr.length; i ++){
			if(arr[i].equals("PATH_SPLIT")){
				arr[i] = dr_node;
			}
		}
		for(int k = 0;k < arr.length; k ++){
			if(k != arr.length - 1){
				path_tmp += arr[k];
				path_tmp += "/";
			}
			else
			{
				path_tmp += arr[k];
			}
		}
		return path_tmp;
	}
	
	/** 
     * 获得指定日期的前一天 
     *  
     * @param specifiedDay 
     * @return 
     * @throws Exception 
     */  
    public static String getSpecifiedDayBefore(String specifiedDay) {//可以用new Date().toLocalString()传递参数  
        Calendar c = Calendar.getInstance();  
        Date date = null;  
        try {  
            date = new SimpleDateFormat("yyyyMMdd").parse(specifiedDay);  
        } catch (ParseException e) {
        }  
        c.setTime(date);  
        int day = c.get(Calendar.DATE);  
        c.set(Calendar.DATE, day - 1);  
  
        String dayBefore = new SimpleDateFormat("yyyyMMdd").format(c  
                .getTime());  
        return dayBefore;  
    }  
    
    /** 
     * 获得指定日期的后一天 
     *  
     * @param specifiedDay 
     * @return 
     */  
    public static String getSpecifiedDayAfter(String specifiedDay) {  
        Calendar c = Calendar.getInstance();  
        Date date = null;  
        try {  
            date = new SimpleDateFormat("yyyyMMdd").parse(specifiedDay);  
        } catch (ParseException e) { 
        }  
        c.setTime(date);  
        int day = c.get(Calendar.DATE);  
        c.set(Calendar.DATE, day + 1);  
  
        String dayAfter = new SimpleDateFormat("yyyyMMdd")  
                .format(c.getTime());  
        return dayAfter;  
    }
    
    public static String getSpecifiedDayAfter(String specifiedDay,int days) {  
        Calendar c = Calendar.getInstance();  
        Date date = null;  
        try {  
            date = new SimpleDateFormat("yyyyMMdd").parse(specifiedDay);  
        } catch (ParseException e) { 
        }  
        c.setTime(date);  
        int day = c.get(Calendar.DATE);  
        c.set(Calendar.DATE, day + days);  
  
        String dayAfter = new SimpleDateFormat("yyyyMMdd")  
                .format(c.getTime());  
        return dayAfter;  
    }
    
    
    public static Date getDateByString(String strDate,String format){
    	Date dt = new Date();
    	f = new SimpleDateFormat(format);
    	try {
			dt = (Date)f.parseObject(strDate);
		} catch (ParseException e) {
		}
    	return dt;
    }
	
}
